
package com.zsrt.day20170424.homework;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

import org.junit.Test;
public class WriterTest {
	
	/**
	 *  使用 OutputStreamWriter 把  把 "中深软通123" 写入shuchu BufferedWriter.txt
	 */
	@Test
	public void  OutputStreamWriterTest() {
		
		OutputStream os = null;;
		Writer w =null;;
		try {
			//文件
			File file = new File("shuchu/BufferedWriter.txt");
			
			//字节流
			os = new FileOutputStream(file);
			
			//字符流
			w = new OutputStreamWriter(os);
			
			String string ="中深软通123";
			
			w.write(string);
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}finally {
			if(os != null && w != null){
				
				try {
					//关闭流
					w.close();
					os.close();
					
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
			
		}
		
	}

	/**
	 * 把shuchu下的aa.txt中的内容复制到bb.txt  如果是中文会有乱码问题
	 */
	@Test
	public void WriterTest01(){
		
		FileInputStream is =null;
		FileOutputStream os = null;
		try {
			File file = new File("shuchu/aa.txt");
			
			//要复制的字节流
			is = new FileInputStream(file);
			
			//复制的目的地
			File file1 = new File("shuchu/bb.txt");
			
			os = new FileOutputStream(file1 );
			int i = 0;
			try {
				//一个一个的读取和写入
				while((i = is.read())!=-1){
					
					os.write(i);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally {
			if(os != null && is !=null){
				
				try {
					os.close();
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}	
	}
	
	/**
	 * 使用字符流 把 shuchu下的aaa.txt中的内容复制到bbb.txt  如果是中文会有乱码问题
	 */
	@Test
	public void test01() {
		
		Reader r =null;
		Writer w = null;
		try {
			//被复制的文件
			File file = new File("shuchu/aaa.txt");
			r = new FileReader(file);

			//目标文件
			File file2 = new File("shuchu/bbb.txt");
			w = new FileWriter(file2);
			
			//一个一个的读取写入
			int i =-1;
			while((i=r.read())!=-1){
				w.write(i);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(w !=null  && r !=null){
				//关闭字符流
				try {
					w.close();
					r.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 使用字节流 把 shuchu下的demo01.png中的内容复制到demo02.png  而且要能正常打开 而且大小也一样
	 */
	@Test
	public void pngTest(){
		
		InputStream is = null;
		OutputStream os = null;
		try {
			//被复制的图形文件
			File file = new File("shuchu/demo01.png");
			//目标文件
			File file1 = new File("shuchu/demo02.png");
			
			is = new FileInputStream(file);
			
			os = new FileOutputStream(file1);
			
			int i =0;
			//读取和写入
			while((i = is.read()) != -1){
				os.write(i);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(os != null && is !=null){
				try {
					//关闭流
					os.close();
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	/**
	 * 使用Writer抽象类 (FileWriter) 把  "中深软通123" 写入到一个文件   shuchu cc.txt
	 */
	@Test
	public void fileWriterTest(){
		
		Writer fw=null;
		try {
			File file = new File("shuchu/cc.txt");
			
			fw = new FileWriter(file);
			
			String string= "中深软通123";
			//写入
			fw.write(string);
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(fw !=null){
				try {
					//关闭流
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
	/**
	 * 使用BufferedWriter  把 "中深软通123" 写入shuchu BufferedWriter2.txt
	 */
	@Test
	public void testName(){
		
		Writer w = null;
		try {
			
			File file = new File("shuchu/BufferedWriter2.txt");
			
			FileWriter out = new FileWriter(file);
			
			w = new BufferedWriter(out );
			
			String string= "中深软通123";
			//写入
			w.write(string);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(w != null){
				
				try {
					//关闭文件输出流
					w.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
}

